<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\Grid\Deposit\DepositStateGrid;
use App\Admin\Actions\Grid\Deposit\DepositSuccessGrid;
use App\Admin\Repositories\UserDeposit;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;

class UserDepositController extends AdminBaseController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new UserDeposit(['user']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $this->gridFilter($grid, 'user_deposits');
            $grid->column('id')->sortable();
            $grid->column('order_sn', __("订单号"))->sortable();
            $grid->column('uid')->display(function($uid) {
                return ($this->user && $this->user->email ? "{$this->user->email}<br/>" : "") . "ID:{$uid}";
            });

            $grid->combine('_from', [
                'from_symbol',
                'from_amount',
            ], __("来源"))->class("text-center");

            $grid->combine('_to', [
                'to_symbol',
                'to_amount',
                'to_real_amount'
            ], __("目标"))->class("text-center");

            $grid->column('from_symbol');
            $grid->column('from_amount')->numberFormat();
            $grid->column('to_symbol');
            $grid->column('to_amount')->numberFormat();
            $grid->column('to_real_amount',__("实际到账"))->numberFormat();
            $grid->column('rate')->numberFormat();
            $grid->column('user_account');
            $grid->column('user_name');
            $grid->column('to_address');
            $grid->column('status')->using(tt_deposit('', true))->label([
                DEPOSIT_COMMIT => 'primary',
                DEPOSIT_SUCCESS => 'success',
                DEPOSIT_FAIL => 'danger',
            ]);
            $grid->column('remark');
            $grid->column('image')->image(g_admin_image(), 30, 30);


            $grid->column('_chain', __("链信息"))->display(function () {
                return
                    "执行时间： {$this->executed_at}<br/>" .
                    "哈希： " . $this->executed_tx;
            });
//            $grid->column('executed_tx');
//            $grid->column('executed_at');
            $grid->column('created_at');
//            $grid->column('updated_at')->sortable();


            $grid->disableViewButton();
            $grid->disableEditButton();
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                if ($this->status == DEPOSIT_COMMIT) {
//                    $success = new DepositStateGrid();
//                    $success->setInput(['status' => DEPOSIT_SUCCESS]);
//                    $actions->append($success);

                    $actions->append(new DepositSuccessGrid());
                    $fail = new DepositStateGrid();
                    $fail->setInput(['status' => DEPOSIT_FAIL]);
                    $actions->append($fail);
                }
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id')->width(3);
                $filter->equal('uid')->width(3);
                $filter->equal('from_symbol')->width(3);
                $filter->equal('to_symbol')->width(3);
                $filter->between('created_at')->datetime()->width(6);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserDeposit(), function (Show $show) {
            $show->field('id');
            $show->field('uid');
            $show->field('from_symbol');
            $show->field('from_amount');
            $show->field('to_symbol');
            $show->field('to_amount');
            $show->field('rate');
            $show->field('user_account');
            $show->field('user_name');
            $show->field('to_address');
            $show->field('status');
            $show->field('remark');
            $show->field('image');
            $show->field('executed_tx');
            $show->field('executed_at');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserDeposit(), function (Form $form) {
            $form->display('id');
            $form->text('uid');
            $form->text('from_symbol');
            $form->text('from_amount');
            $form->text('to_symbol');
            $form->text('to_amount');
            $form->text('rate');
            $form->text('user_account');
            $form->text('user_name');
            $form->text('to_address');
            $form->switch('status');
            $form->text('remark');
            $form->text('image');
            $form->text('executed_tx');
            $form->text('executed_at');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
